(function($){
	var createGridHeaderContextMenu = function(e, field) {
		e.preventDefault();
		var datagrid = $(this).data('datagrid');
		var state = $.data(this, 'datagrid');
		var grid = $(this);/* grid本身 */
		var headerContextMenu = this.headerContextMenu;/* grid上的列头菜单对象 */
		var okCls = 'tree-checkbox1';// 选中
		var emptyCls = 'tree-checkbox0';// 取消
		if (!headerContextMenu) {
			var storageKey = "grid_fields_" + grid.attr("id") + "_" + window.location.pathname;
			var storageData = JSON.parse(window.localStorage.getItem(storageKey)),
				isNull = false;
			if(!storageData){
				storageData = {};
				isNull = true;
			}
			var tmenu = $('<div style="width:100px;"></div>').appendTo('body');
			var fields = grid.datagrid('getColumnFields').concat(grid.datagrid('getColumnFields',true));
			for (var i = 0; i < fields.length; i++) {
				var fildOption = storageData[fields[i]] || grid.datagrid('getColumnOption', fields[i]);
				if(fildOption.checkbox) continue;
				if (!fildOption.hidden) {
					$('<div iconCls="' + okCls + '" field="' + fields[i] + '"/>')
						.html(fildOption.title).appendTo(tmenu);
				} else {
					$('<div iconCls="' + emptyCls + '" field="' + fields[i] + '"/>')
						.html(fildOption.title).appendTo(tmenu);
				}
				storageData[fields[i]] = fildOption;
			}
			headerContextMenu = this.headerContextMenu = tmenu.menu({
				onClick : function(item) {
					var field = $(item.target).attr('field');
					if (item.iconCls == okCls) {
						grid.datagrid('hideColumn', field);
						$(this).menu('setIcon', {
							target : item.target,
							iconCls : emptyCls
						});
					} else {
						grid.datagrid('showColumn', field);
						$(this).menu('setIcon', {
							target : item.target,
							iconCls : okCls
						});
					}
					if(!isNull) {
						storageData[field].hidden = !storageData[field].hidden;
					}
					window.localStorage.setItem(storageKey, JSON.stringify(storageData));
				}
			});
		}
		headerContextMenu.menu('show', {
			left : e.pageX,
			top : e.pageY
		});
	};
	$.fn.datagrid.defaults.onHeaderContextMenu = createGridHeaderContextMenu;
	$.fn.treegrid.defaults.onHeaderContextMenu = createGridHeaderContextMenu;
	
	var datagrid_autoSizeColumn = $.fn.datagrid.methods.autoSizeColumn;
	var treegrid_autoSizeColumn = $.fn.treegrid.methods.autoSizeColumn;
	
	function isDatagrid(target){
		if ($(target).data('treegrid')){
			return false;
		} else {
			return true;
		}
	}
	
	var autoSizeColumn = function(jq, _1ed){
		var grid = $(jq);/* grid本身 */
		var storageKey = "grid_fields_" + grid.attr("id") + "_" + window.location.pathname;
		var storageData = JSON.parse(window.localStorage.getItem(storageKey)) || {};
		
		var fields = grid.datagrid('getColumnFields').concat(grid.datagrid('getColumnFields',true));
		for (var i = 0; i < fields.length; i++) {
			var fildOption = storageData[fields[i]];
			if(fildOption) {
				if (!fildOption.hidden) {
					grid.datagrid('showColumn', fields[i]);
				} else {
					grid.datagrid('hideColumn', fields[i]);
				}
			}
		}
		
		var boo = isDatagrid(this);
		if(boo) return datagrid_autoSizeColumn.call(this,jq, _1ed); else return treegrid_autoSizeColumn.call(this,jq, _1ed);
	}
	$.fn.datagrid.methods.autoSizeColumn = autoSizeColumn;
	$.fn.treegrid.methods.autoSizeColumn = autoSizeColumn;
})(jQuery);
